swadhinbiswas Fix: Add date validation in analysis.ts to prevent RangeError · 1/20/2026
a7662cd
99 lines · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
 * AI Review Rules PATCH/DELETE API
 * Update and delete operations for custom AI review rules
 */

import type { APIRoute } from "astro";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { eq } from "drizzle-orm";
import { getDatabase, schema } from "@/db";

export const PATCH: APIRoute = async ({ locals, request, params }) => {
    const user = locals.user;

    if (!user) {
        return new Response(JSON.stringify({ error: "Unauthorized" }), {
            status: 401,
            headers: { "Content-Type": "application/json" },
        });
    }

    const ruleId = params.id;
    if (!ruleId) {
        return new Response(JSON.stringify({ error: "Rule ID required" }), {
            status: 400,
            headers: { "Content-Type": "application/json" },
        });
    }

    try {
        const body = await request.json();
        const db = getDatabase() as NodePgDatabase<typeof schema>;

        const updates: Record<string, unknown> = {
            updatedAt: new Date(),
        };

        if (body.name !== undefined) updates.name = body.name;
        if (body.description !== undefined) updates.description = body.description;
        if (body.ruleType !== undefined) updates.ruleType = body.ruleType;
        if (body.aiPrompt !== undefined) updates.aiPrompt = body.aiPrompt;
        if (body.regexPattern !== undefined) updates.regexPattern = body.regexPattern;
        if (body.severity !== undefined) updates.severity = body.severity;
        if (body.category !== undefined) updates.category = body.category;
        if (body.fileGlobs !== undefined) updates.fileGlobs = body.fileGlobs;
        if (body.isEnabled !== undefined) updates.isEnabled = body.isEnabled;

        await db
            .update(schema.aiReviewRules)
            .set(updates)
            .where(eq(schema.aiReviewRules.id, ruleId));

        return new Response(JSON.stringify({ success: true }), {
            status: 200,
            headers: { "Content-Type": "application/json" },
        });
    } catch (error) {
        return new Response(JSON.stringify({ error: "Failed to update rule" }), {
            status: 500,
            headers: { "Content-Type": "application/json" },
        });
    }
};

export const DELETE: APIRoute = async ({ locals, params }) => {
    const user = locals.user;

    if (!user) {
        return new Response(JSON.stringify({ error: "Unauthorized" }), {
            status: 401,
            headers: { "Content-Type": "application/json" },
        });
    }

    const ruleId = params.id;
    if (!ruleId) {
        return new Response(JSON.stringify({ error: "Rule ID required" }), {
            status: 400,
            headers: { "Content-Type": "application/json" },
        });
    }

    try {
        const db = getDatabase() as NodePgDatabase<typeof schema>;
        await db
            .delete(schema.aiReviewRules)
            .where(eq(schema.aiReviewRules.id, ruleId));

        return new Response(JSON.stringify({ success: true }), {
            status: 200,
            headers: { "Content-Type": "application/json" },
        });
    } catch (error) {
        return new Response(JSON.stringify({ error: "Failed to delete rule" }), {
            status: 500,
            headers: { "Content-Type": "application/json" },
        });
    }
};